<?php
/**
 * PM5 PHP Framework
 * Originally developed by Patrick McGovern
 *
 */
class CoreFunctions 
{
	public function __construct($main)
	{
		/**
		 * reference the main construct of the application
		 */
		$this->main = $main;
		
		return true;
	}
	
	/**
	 * Determine if current user is connecting via a 
	 * mobile browser
	 *
	 */
	public function isMobileBrowser()
	{
		if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
			return true;
		}
		 
		if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
			return true;
		}		
		 
		$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
		
		$mobile_agents = array(
				'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
				'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
				'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
				'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
				'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
				'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
				'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
				'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
				'wapr','webc','winw','winw','xda','xda-');
		 
		if(in_array($mobile_ua,$mobile_agents)) {
			return true;
		}
		 
		if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) {
			return true;
		}
		 
		if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
			return false;
		}
		
		return false;
	}
	
	/**
	 * Generate Pagination HTML based off of criteria:
	 * 	- Current selected page (passed parameter)
	 *  - item list (array of data)
	 *
	 * Return startID & endID to reference from the item list indicating
	 * which items to diaply on the current page, along with the
	 * generated paginationHTML, all wrapped in an array.
	 *
	 *
	 * @param int $itemCount
	 * @return array
	 */
	public function generatePaginationData($itemCount)
	{
		/**
		 * Determine current Page & total page count
		 */
		$page = (!empty($this->main->passed->page)) ? $this->main->passed->page : 1;
		$totalPageCount = ceil($itemCount / PAGINATION_ITEMS_PER_PAGE);

		/**
		 * Return an empty string if the current page is the only page
		 */
		if ($totalPageCount == 1){
			return array(0,$itemCount,'');
		}

		/**
		 * Set the start ID
		 */
		$startID = ($page * PAGINATION_ITEMS_PER_PAGE) - PAGINATION_ITEMS_PER_PAGE;
		$startID = ($page > 1) ? ($startID + 1) : $startID;
		$startID = ($startID < 5) ? 0 : $startID;

		/**
		 * Modify the page number if we are beyond our results
		 */
		if ($startID > ($itemCount - 1)){
			$page = $totalPageCount;
		}

		/**
		 * Set the end ID
		 */
		$endID = $startID + PAGINATION_ITEMS_PER_PAGE;

		/**
		 * Modify the end ID if it goes beyond our results set
		 */
		$endID = ($endID > ($itemCount - 1)) ? ($itemCount - 1) : $endID;

		/**
		 * Generate the pagination HTML
		 * Previous 1 ... 12 13 14 ... 326 Next
		 * if Page < 5:
		 * Previous 1 2 3 4 5 ... 326 Next
		 */

		/**
		 * Remove page number & trailing ampersand from current URL
		 */
		$requestURI = preg_replace('#page=[0-9]*#','',$_SERVER['REQUEST_URI']);
		$requestURI = (substr($requestURI,-1) == '&') ? substr($requestURI,0,-1) : $requestURI;

		$html  = "<div class=\"pagination\">\n";
		$html .= "	<ul>\n";

		/**
		 * 'Previous' link
		 */
		if ($page > 1){
			$prevPage = ($page - 1);

			$html .= $this->_paginationlistItem(true,$requestURI,$prevPage,'Previous');
		}

		/**
		 * Display all page links if there are less than 6 pages
		 * Display first 5 page links if current page is 5 or less
		 * Display last 5 page links if current page is withing 5 of total page count
		 * Otherwise display link to page 1, curent page including 2 prev & 2 next & last page
		 */
		if ($totalPageCount < 6)
		{
			$html .= ($page == 1) ? $this->_paginationlistItem(false,$requestURI,1) : $this->_paginationlistItem(true,$requestURI,1);

			if ($totalPageCount >= 2){
				$html .= ($page == 2) ? $this->_paginationlistItem(false,$requestURI,2) : $this->_paginationlistItem(true,$requestURI,2);
			}

			if ($totalPageCount >= 3){
				$html .= ($page == 3) ? $this->_paginationlistItem(false,$requestURI,3) : $this->_paginationlistItem(true,$requestURI,3);
			}

			if ($totalPageCount >= 4){
				$html .= ($page == 4) ? $this->_paginationlistItem(false,$requestURI,4) : $this->_paginationlistItem(true,$requestURI,4);
			}

			if ($totalPageCount >= 5){
				$html .= ($page == 5) ? $this->_paginationlistItem(false,$requestURI,5) : $this->_paginationlistItem(true,$requestURI,5);
			}
		}
		elseif ($page < 6){
			$html .= ($page == 1) ? $this->_paginationlistItem(false,$requestURI,1) : $this->_paginationlistItem(true,$requestURI,1);
			$html .= ($page == 2) ? $this->_paginationlistItem(false,$requestURI,2) : $this->_paginationlistItem(true,$requestURI,2);
			$html .= ($page == 3) ? $this->_paginationlistItem(false,$requestURI,3) : $this->_paginationlistItem(true,$requestURI,3);
			$html .= ($page == 4) ? $this->_paginationlistItem(false,$requestURI,4) : $this->_paginationlistItem(true,$requestURI,4);
			$html .= ($page == 5) ? $this->_paginationlistItem(false,$requestURI,5) : $this->_paginationlistItem(true,$requestURI,5);

			$html .= " ... \n";

			/**
			 * Last page
			 */
			$html .= $this->_paginationlistItem(true,$requestURI,$totalPageCount);
		}
		elseif (($page + 5) > $totalPageCount){
			/**
			 * Display last 5 page links if current page is withing 5 of total page count
			 */
			$tpcm1 = ($totalPageCount - 1);
			$tpcm2 = ($totalPageCount - 2);
			$tpcm3 = ($totalPageCount - 3);
			$tpcm4 = ($totalPageCount - 4);

			$html .= $this->_paginationlistItem(true,$requestURI,1);
			$html .= "		 ... \n";
			$html .= ($page == $tcpm4) ? $this->_paginationlistItem(false,$requestURI,$tcpm4) : $this->_paginationlistItem(true,$requestURI,$tcpm4);
			$html .= ($page == $tcpm3) ? $this->_paginationlistItem(false,$requestURI,$tcpm3) : $this->_paginationlistItem(true,$requestURI,$tcpm3);
			$html .= ($page == $tcpm2) ? $this->_paginationlistItem(false,$requestURI,$tcpm2) : $this->_paginationlistItem(true,$requestURI,$tcpm2);
			$html .= ($page == $tcpm1) ? $this->_paginationlistItem(false,$requestURI,$tcpm1) : $this->_paginationlistItem(true,$requestURI,$tcpm1);

			$html .= ($page == $totalPageCount) ? $this->_paginationlistItem(false,$requestURI,$totalPageCount) : $this->_paginationlistItem(true,$requestURI,$totalPageCount);
		}
		else {
			/**
			 * Display link to page 1, curent page including 2 prev & 2 next & last page
			 */
			$pm1 = ($page - 1);
			$pm2 = ($page - 2);
			$pp1 = ($page + 1);
			$pp2 = ($page + 2);

			$html .= $this->_paginationlistItem(true,$requestURI,1);
			$html .= "		 ... \n";
			$html .= $this->_paginationlistItem(true,$requestURI,$pm2);
			$html .= $this->_paginationlistItem(true,$requestURI,$pm1);
			$html .= $this->_paginationlistItem(false,$requestURI,$page);
			$html .= $this->_paginationlistItem(true,$requestURI,$pp1);
			$html .= $this->_paginationlistItem(true,$requestURI,$pp2);
			$html .= "		 ... \n";
			$html .= $this->_paginationlistItem(true,$requestURI,$totalPageCount);
		}

		/**
		 * 'Next' link
		 */
		if ($page < $totalPageCount){
			$nextPage = ($page + 1);

			$html .= $this->_paginationlistItem(true,$requestURI,$nextPage,'Next');
		}

		$html .= "	</ul>";
		$html .= "</div>";

		return array($startID,$endID,$html);
	}
	
	/**
	 * Child function of generatePaginationData()
	 *
	 * @param boolean $link
	 * @param varchar $requestURI URI for link (excluding page)
	 * @param int $page
	 * @return unknown
	 */
	private function _paginationlistItem($link,$requestURI,$page,$linkText='')
	{
		$linkText = (!empty($linkText)) ? $linkText : $page;
		$val = ($link === true) ? "		<li><a href=\"{$requestURI}&page={$page}\">{$linkText}</a></li>\n" : "		<li class=\"active\">{$linkText}</li>\n";

		return $val;
	}
}